/************************************************************************************/
/*  Program name CAPFNDIMP_REQ_SET_STAGE.sql                                        */
/*  Creater:  Chan Billy Ka Keung (PCCW - UniHub)                                   */
/*************   History   *************************                                */
/* 01 FEB, 2006   CHAN Billy Ka Keung         First created                         */
/************************************************************************************/

SET SERVEROUTPUT ON SIZE 1000000;

DECLARE

	lv_PrOG_ID		VARCHAR2(100) 	:=	'CAPFNDIMP_REQ_SET_STAGE';

	lv_Scc_Count	NUMBER			:=	0;
	lv_Err_Count	NUMBER			:=	0;
	lv_Err_Msg		VARCHAR2(500)	:=	'No Error';

	CURSOR Cur_F IS
	SELECT
		*
	FROM
		CRST_FND_REQ_SET_STAGE;

	lv_Next_Seq_ID	NUMBER;

BEGIN
	--RENAME
	UPDATE	FND_REQUEST_SET_STAGES
	SET		Stage_Name = REQUEST_SET_ID||Stage_Name
	WHERE	(Set_Application_ID,Request_Set_Stage_ID,Request_Set_ID,Stage_Name) IN
							(
							SELECT
								Set_Application_ID,Request_Set_Stage_ID,Request_Set_ID,Stage_Name
							FROM
								CRST_FND_REQ_SET_STAGE
							);

	FOR C1 IN Cur_F LOOP
		BEGIN
			SELECT	FND_REQUEST_SET_STAGES_S.NextVal
			INTO	lv_Next_Seq_ID
			FROM	DUAL;

			INSERT INTO CRST_FND_REQ_SET_STAGE_MAP
			VALUES
			(
			C1.SET_APPLICATION_ID,
			C1.REQUEST_SET_ID,
			C1.REQUEST_SET_STAGE_ID,
			C1.STAGE_NAME,
			C1.SET_APPLICATION_ID,			
			CAPBRIMP_MAPPING.GetRequestSetIDMapping(C1.REQUEST_SET_ID),
			lv_Next_Seq_ID,			
			CAPBRIMP_MAPPING.GetRequestSetIDMapping(C1.REQUEST_SET_ID)||C1.STAGE_NAME
			);

			INSERT INTO FND_REQUEST_SET_STAGES
			VALUES
			(
			C1.SET_APPLICATION_ID,
			CAPBRIMP_MAPPING.GetRequestSetIDMapping(C1.REQUEST_SET_ID),		--REQUEST_SET_ID,
			lv_Next_Seq_ID,													--REQUEST_SET_STAGE_ID,
			C1.STAGE_NAME,
			C1.LAST_UPDATE_DATE,
			CAPBRIMP_COMMON.GetUser(C1.LAST_UPDATED_BY_NAME),				--LAST_UPDATED_BY
			C1.CREATION_DATE,
			CAPBRIMP_COMMON.GetUser(C1.CREATED_BY_NAME),					--CREATED_BY
			C1.LAST_UPDATE_LOGIN,
			C1.CRITICAL,
			C1.OUTCOME,
			C1.ALLOW_CONSTRAINTS_FLAG,
			C1.DISPLAY_SEQUENCE,
			C1.FUNCTION_APPLICATION_ID,
			C1.FUNCTION_ID,
			C1.SUCCESS_LINK,
			C1.WARNING_LINK,
			C1.ERROR_LINK,
			CAPBRIMP_COMMON.GetFNDConcPrgID(C1.CONCURRENT_PROGRAM_ID_D),	--CONCURRENT_PROGRAM_ID,
			C1.X,
			C1.Y,
			C1.ICON_NAME
			);
		EXCEPTION
			WHEN OTHERS THEN
				DBMS_OUTPUT.Put_lINe(SQLERRM);
				lv_Err_Count	:=	lv_Err_Count + 1;
		END;
		lv_Scc_Count		:= lv_Scc_Count + 1;
	END LOOP;

	BEGIN
		UPDATE	FND_REQUEST_SET_STAGES
		SET		Success_Link	=	CAPBRIMP_MAPPING.GetRequestSetStageIDMapping(Success_Link)
		WHERE	Success_Link			IS	NOT NULL
		AND		Request_Set_Stage_ID	IN
						(
							SELECT	Request_Set_Stage_ID_N
							FROM	CRST_FND_REQ_SET_STAGE_MAP
						);

		UPDATE	FND_REQUEST_SET_STAGES
		SET		Warning_Link	=	CAPBRIMP_MAPPING.GetRequestSetStageIDMapping(Warning_Link)
		WHERE	Warning_Link			IS	NOT NULL
		AND		Request_Set_Stage_ID	IN
						(
							SELECT	Request_Set_Stage_ID_N
							FROM	CRST_FND_REQ_SET_STAGE_MAP
						);
	EXCEPTION
		WHEN OTHERS THEN
			DBMS_OUTPUT.Put_lINe(SQLERRM);
			lv_Err_Count	:=	lv_Err_Count + 1;
	END;

	IF lv_Err_Count	>	0	THEN
		ROLLBACK;
	END IF;

	INSERT INTO CAPCNV_LOG(
		PROG_ID,
		PROCESS_DATE,
		RECORD_PROCESSED,
		RECORD_REJECTED,
		RECORD_EXTRACTED,
		ERR_MSG
	)
	VALUES (
		lv_Prog_ID,
		SYSDATE,
		lv_Scc_Count,
		lv_Err_Count,
		lv_Scc_Count - lv_Err_Count,
		lv_Err_Msg
	);

	COMMIT;
END;
/
